package com.atguigu.gmall.publisher.controller;

import com.atguigu.gmall.publisher.bean.TradeProvinceOrderAmount;
import com.atguigu.gmall.publisher.service.TradeStatsService;
import com.atguigu.gmall.publisher.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Felix
 * @date 2023/9/4
 * 交易域统计Controller
 * @Controller 将当前类对象的创建交给Spring的IOC容器，如果当前类中的方法返回的是String，会进行页面的跳转,
 * 如果不想进行跳转，可以在方法上加一个注解@ResponseBody
 * @RestController 将当前类对象的创建交给Spring的IOC容器，如果当前类中的方法返回的是String，不会进行页面的跳转,
 * 只是将字符串相应给客户端。
 * @RestController = @Controller + @ResponseBody
 * @RequestMapping("/gmv") 拦截请求，将请求交给注解标准的方法进行处理
 * @RequestParam 接收请求中携带的参数，并对其进行默认值的处理
 * @Autowired 注入  从容器中找到对应的类型的对象，赋值给当前属性
 */
@RestController
public class TradeStatsController {

    @Autowired
    private TradeStatsService tradeStatsService;

    @RequestMapping("/gmv")
    public String getGMV(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            //如果请求中没有传递日期参数，将当前日期作为查询日期
            date = DateFormatUtil.now();
        }
        //调用service获取总交易额
        BigDecimal gmv = tradeStatsService.getGMV(date);
        String json = "{\"status\": 0,\"data\": " + gmv + "}";

        return json;
    }

    /*@RequestMapping("/province")
    public String getProvinceAmount(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TradeProvinceOrderAmount> provinceOrderAmountList = tradeStatsService.getProvinceAmount(date);

        StringBuilder jsonB = new StringBuilder("{\"status\": 0,\"data\": {\"mapData\": [");
        for (int i = 0; i < provinceOrderAmountList.size(); i++) {
            TradeProvinceOrderAmount provinceOrderAmount = provinceOrderAmountList.get(i);
            jsonB.append(" {\"name\": \"" + provinceOrderAmount.getProvinceName() + "\",\"value\": " + provinceOrderAmount.getOrderAmount() + "}");
            if(i < provinceOrderAmountList.size() - 1){
                jsonB.append(",");
            }
        }
        jsonB.append("],\"valueName\": \"交易额\"}}");
        return jsonB.toString();
    }*/
    @RequestMapping("/province")
    public Map getProvinceAmount(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TradeProvinceOrderAmount> provinceOrderAmountList = tradeStatsService.getProvinceAmount(date);
        Map resMap = new HashMap();
        resMap.put("status",0);

        Map dataMap = new HashMap();
        List dataList = new ArrayList();
        for (TradeProvinceOrderAmount provinceOrderAmount : provinceOrderAmountList) {
            Map map = new HashMap();
            map.put("name",provinceOrderAmount.getProvinceName());
            map.put("value",provinceOrderAmount.getOrderAmount());
            dataList.add(map);
        }
        dataMap.put("mapData",dataList);
        dataMap.put("valueName","交易额");
        resMap.put("data",dataMap);
        return resMap;
    }
}
